import { adminNews } from "@/api/content.js";

export let adNews = {
    namespaced: true,
    state: () => ({ 
        news: [],
        editNews: {
            id: '',
            title: "",
            introduce: "",
            thumb: "",
            content: "",
            keywords: "",
        },
        currentTabs: "data",
        showEdit: false,
        loading: false,
        editID: -1,
        count: 0
     }),
    mutations: { 
        setLoading(state, flag) {
            state.loading = flag
        },
        async getNews(state, page){
            if(state.loading)return 
            state.loading = true
            let data = await adminNews.getAll(page);
            if (data.status == 200){
                state.news = data.news
            }
            state.count = data.count
            state.loading = false
        },
        async setEditNews(state, scope){
            state.loading = true
            state.editID = scope.index_
            console.log('row_index', scope.index_)
            let data = await adminNews.getNews(scope.row.id)
            console.log('news', data)
            state.loading = false
            state.editNews = data;
            state.showEdit = true
            state.currentTabs = "edit"
        },
        changeNews(state, news){
            if (state.row_index === -1)return
            console.log(state.editID)
            state.news[state.editID] = news;
            
            state.showEdit = false;
            state.currentTabs = "data";
            state.editNews = {
                id: '',
                title: "",
                introduce: "",
                thumb: "",
                content: "",
                keywords: "",
            };
        },
        spliceNews(state, row_index){
            console.log('tag', row_index)
            state.news.splice(row_index, 1);
        },
        cancelEdit(state){
            state.showEdit = false;
            state.currentTabs = "data";
            state.editNews = {
                id: '',
                title: "",
                introduce: "",
                thumb: "",
                content: "",
                keywords: "",
            };
        },
        unshiftNews(state, news){
            state.news.unshift(news)
        },
        
    },
    actions: {
        setEditNewsAction(context, scope){
            context.commit('setEditNews', scope)
        }
    }
}